import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freedom/const/app_content.dart';
import 'package:freedom/entity/mahjong/mahjong_room_record.dart';
import 'package:freedom/page/mahjong/bloc/room_bloc.dart';
import 'package:freedom/page/mahjong/common/mahjong_context.dart';
import 'package:freedom/page/mahjong/dialog/change_card_dialog.dart';
import 'package:freedom/page/mahjong/dialog/game_message.dart';
import 'package:freedom/page/mahjong/dialog/play_back_dialog.dart';
import 'package:freedom/page/mahjong/dialog/record_dialog.dart';
import 'package:freedom/page/mahjong/keneck/hu_card_dialog.dart';
import 'package:freedom/page/mahjong/keneck/game_over_dialog.dart';
import 'package:freedom/page/mahjong/keneck/multiple_choice_card.dart';
import 'package:freedom/page/mahjong/keneck/user_info_dialog.dart';
import 'package:freedom/page/mahjong/swap_card.dart';
import 'package:freedom/util/socket_server.dart';
import 'mahjong_game.dart';

class GamingIndexPage extends StatefulWidget {
  final String roomCode;
  final Object? record;
  const GamingIndexPage(this.roomCode, {super.key, this.record});

  @override
  State<StatefulWidget> createState() => GamingIndexWidget();
}

class GamingIndexWidget extends State<GamingIndexPage> {
  @override
  void dispose() {
    super.dispose();
    SocketServer().closeSocket();
  }

  Object? get record => widget.record;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<RoomBloc>(create: (_) => RoomBloc()),
      ],
      child: GamingWidget(widget.roomCode, record: record),
    );
  }
}

class GamingWidget extends StatelessWidget {
  final String roomCode;

  final Object? record;
  const GamingWidget(this.roomCode, {this.record});

  @override
  Widget build(BuildContext context) {
    return GameWidget<MahjongIndexGame>(
      game: MahjongIndexGame(
          roomBloc: context.read<RoomBloc>(),
          roomCode: roomCode,
          record: record),
      initialActiveOverlays: const [
        MahjongOverlay.userInfoDialog,
      ],
      overlayBuilderMap: {
        MahjongOverlay.MAHJONG_GAMEING_OVER: (context, game) {
          var gameInfo = game.gameInfo;
          return Settlement(gameInfo);
        },
        MahjongOverlay.userInfoDialog: (context, game) {
          return UserInfoDialog(
            game: game,
          );
        },
        MahjongOverlay.huCardDialog: (context, game) {
          return HuCardDialog(
            game: game,
          );
        },
        MahjongOverlay.swapCardTipsDialog: (context, game) {
          return ChangeCardDialog(
            game: game,
          );
        },
        MahjongOverlay.multipmleChoiceCardDialog: (context, game) {
          return MultipleChoiceCardDialog(
            game: game,
          );
        },
        MahjongOverlay.swapCardInfo: (context, game) {
          return SwapCard(
            game: game,
          );
        },
        MahjongOverlay.balanceDialog: (context, game) {
          return RecodeDialog(
            game: game,
          );
        },
        SystemOverlay.messageInfoDialog: (context, game) {
          return GameMessage(
            game: game,
          );
        },
        SystemOverlay.playBackDialog: (context, game) {
          return PlayBackDialog(
            game: game,
          );
        } /* ,
        "ALLUSERAV": (context, game) {
          return AllUserAvWidget();
        }, */
      },
    );
  }
}
